package com.base.commons.log.interceptor;

import com.base.commons.tools.utils.StringUtils;
import com.base.commons.log.CommonConstant;
import com.base.commons.log.jaeger.JaegerUtils;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author daiguoping
 * @version 1.00
 * @time 2019/12/26 0026
 * @description
 */
public class TraceInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String traceId = request.getHeader(CommonConstant.TRACE_ID_HEADER);
        if (StringUtils.isNotEmpty(traceId)) {
            MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
        }else{
            traceId = JaegerUtils.getTraceId();
            MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
        }
        return true;
    }
}
